﻿//-----------------------------------------------------------------------
// <copyright file="Manufacturer.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//---------------------------------------------------------------------

namespace Delta.Mercury.Business.Commerce
{
    using System;
    using System.Globalization;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Product manufacturer
    /// </summary>
    [Serializable]
    public class Manufacturer : IdentifierBase, IDataServicable
    {
        #region Members
        /// <summary>
        /// Manufacturer name
        /// </summary>
        private string name;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Manufacturer class.
        /// </summary>
        public Manufacturer()
            : this(string.Empty)
        {
            this.name = string.Empty;
        }

        /// <summary>
        /// Initializes a new instance of the Manufacturer class.
        /// </summary>
        /// <param name="name">Manufacturer name</param>
        public Manufacturer(string name)
            : base()
        {
            this.name = name;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the manufacturer name
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Manufacturer name must be a minimum of 1 characters.");
                }

                if (value.Length > 20)
                {
                    throw new InvalidLengthException("Manufacturer name must be a maximum of 20 characters.");
                }

                TextInfo info = CultureInfo.CurrentCulture.TextInfo;
                this.name = info.ToTitleCase(value);
            }
        }
        #endregion

        #region Static Load Methods
        /// <summary>
        /// Load manufacturer by unique identifier
        /// </summary>
        /// <param name="guid">Unique identifier</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Filled manufacturer</returns>
        public static Manufacturer Load(Guid guid, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", guid));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetSingle<Manufacturer>("LoadManufacturer", System.Data.CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Load manufacturer by namer
        /// </summary>
        /// <param name="name">Manufacturer name</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Filled manufacturer</returns>
        public static Manufacturer Load(string name, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Name", name));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetSingle<Manufacturer>("LoadManufacturer", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Delete Method
        /// <summary>
        /// Delete manufacturer
        /// </summary>
        public void Delete()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            DataAccess.NonQuery("DeleteManufacturer", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Get Images Method
        /// <summary>
        /// Get manufacturer images
        /// </summary>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Collection of images</returns>
        public Images GetImages(bool showDeleted)
        {
            return Images.Load(this.Guid, showDeleted);
        }
        #endregion

        #region Save Method
        /// <summary>
        /// Save manufacturer
        /// </summary>
        public void Save()
        {
            DataAccess.NonQuery("SaveManufacturer", System.Data.CommandType.StoredProcedure, GetSaveParameters());
        }
        #endregion

        #region Fill Method
        /// <summary>
        /// Fill object from database
        /// </summary>
        /// <param name="reader">Data reader</param>
        public void Fill(System.Data.IDataReader reader)
        {
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.name = Helper.ToString(reader["Name"]);
        }
        #endregion

        #region Get Save Parameters
        /// <summary>
        /// Get save parameters
        /// </summary>
        /// <returns>Parameter collection</returns>
        public Parameters GetSaveParameters()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            parameters.Add(new Parameter("Name", this.Guid));
            return parameters;
        }
        #endregion
    }
}
